﻿using System;
using System.Linq;
using System.Web.Mvc;
using Zinnia.Common.Untilty;

namespace Zinnia.Cdn.Controllers
{
    /// <summary>
    /// 压缩
    /// </summary>
    public class CompressController : Controller
    {
        // GET: Compress
        public ActionResult Index(string type, string path)
        {
            if (string.Equals("js", type, StringComparison.OrdinalIgnoreCase))
                return Content(GetContent(path.Split(',')), "text/javascript");
            if (string.Equals("css", type, StringComparison.OrdinalIgnoreCase))
                return Content(GetContent(path.Split(',')), " text/css");
            return Content("");
        }
        string GetContent(string[] paths)
        {
            var strContent = string.Empty;
            if (paths.Any())
            {
                foreach (var p in paths)
                {
                    try
                    {
                        strContent += $"/*{p}*/{FileHelper.Instance.ReadFileText("/Contents/" + p)}\r\n";
                    }
                    catch
                    {
                        strContent += $"/*{p}*/\r\n";
                    }
                }
            }
            return strContent;
        }
    }
}